<?php
namespace ElementVip\JwOms\Providers;

use ElementVip\JwOms\Console\GoodsCommand;
use ElementVip\JwOms\Console\OrderCommand;
use ElementVip\JwOms\Console\RefundCommand;
use ElementVip\JwOms\Console\StockCommand;
use ElementVip\JwOms\Handler\GoodsHandle;
use ElementVip\JwOms\Handler\OrderHandle;
use ElementVip\JwOms\Handler\RefundHandle;
use ElementVip\JwOms\Handler\StockHandle;
use ElementVip\JwOms\Schedule\OmsSchedule;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;


class JwOmsServiceProvider extends ServiceProvider
{

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */

    protected $namespace = 'ElementVip\JwOms\Http\Controllers';


    public function boot()
    {
        if (!$this->app->routesAreCached()) {
            $this->mapWebRoutes();
        }

        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'oms-backend');

        if ($this->app->runningInConsole()) {
            $this->loadMigrationsFrom(__DIR__ . '/../../migrations');
        }

        //$this->mergeFTPConfig();

        $this->commands([
            GoodsCommand::class,
            OrderCommand::class,
            RefundCommand::class,
            StockCommand::class
        ]);
    }

    public function register()
    {

        $this->app->singleton('oms.goods.handle',GoodsHandle::class);
        $this->app->singleton('oms.order.handle',OrderHandle::class);
        $this->app->singleton('oms.refund.handle',RefundHandle::class);
        $this->app->singleton('oms.stock.handle',StockHandle::class);

        if(env('OMS_ENABLED'))
        {
            $this->app->make('ElementVip\ScheduleList')->add(OmsSchedule::class);
        }
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::group([
            'middleware' => ['web', 'auth.admin:admin'],
            'namespace' => $this->namespace,
        ], function ($router) {
            require __DIR__ . '/../Http/routes.php';
        });
    }

    private function mergeFTPConfig()
    {

        $config = $this->app['config']->get('ftp', []);
        $config['connections']['connection1']['host'] = app('system_setting')->getSetting('oms_ftp_host');
        $config['connections']['connection1']['port'] = (int)app('system_setting')->getSetting('oms_ftp_port');
        $config['connections']['connection1']['username'] = app('system_setting')->getSetting('oms_ftp_username');
        $config['connections']['connection1']['password'] = app('system_setting')->getSetting('oms_ftp_password');
        $config['connections']['connection1']['passive'] = app('system_setting')->getSetting('oms_ftp_passive');

        $this->app['config']->set('ftp', $config);


    }

}